package com.hevs.samplewebapp.server.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hevs.samplewebapp.server.entities.User;

/**
 * Mock DAO for the Users
 * 
 * @author Arnaud Durand
 * @date 12.03.12
 */
public class UserDAOMock implements UserDAO{

	private Map<Key ,User> users = new HashMap<Key , User>();
	
	private long seed = 1;
	
	public UserDAOMock() {
		User admin=new User("admin", "81dc9bdb52d04dc20036dbd8313ed055", "admin@hevs.ch", 3, "Arnaud", "Durand", new HashSet<Key>());
		populate(admin, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAQw"));
		User doctor = new User("doctor", "81dc9bdb52d04dc20036dbd8313ed055", "lionel@hevs.ch", 2, "Lionel","Rey", new HashSet<Key>());
		populate(doctor, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAgw"));
		User user1=new User("user1", "81dc9bdb52d04dc20036dbd8313ed055", "thomas@hevs.ch", 1, "Thomas","Rouvinez", new HashSet<Key>());
		populate(user1,KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAww"));
		User user2 =new User("user2", "81dc9bdb52d04dc20036dbd8313ed055", "matthias@hevs.ch", 1, "Matthias","Hurni", new HashSet<Key>());
		populate(user2,KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYBAw"));
		User user3=new User("user3", "81dc9bdb52d04dc20036dbd8313ed055", "peter@hevs.ch", 1, "Peter","Schnidrig", new HashSet<Key>());
		populate(user3,KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYBQw"));
		grantAccess(user1, doctor);
		grantAccess(user2, doctor);
		
	}
	
	private void populate(User user) {
		populate(user, KeyFactory.createKey(
				User.class.getSimpleName(), seed));
	}
	
	
	private void populate(User user, Key key) {
		seed++;
		user.setKey(key);
		users.put(user.getKey(), user);
	}
	
	@Override
	public List<User> getAll() {
		return new ArrayList<User>(users.values());
	}

	@Override
	public void update(User user) {
		if (user.getKey() == null) {
			populate(user);
		} else {
			users.put(user.getKey(), user);
		}
		
	}

	@Override
	public void remove(User user) {
		users.remove(user.getKey());
		
	}

	@Override
	public User getUser(Key key) {
		return users.get(key);
	}
	

	@Override
	public User getUserByLogin(String login) {
		
		for (User user : users.values()) {
			if(user.getLogin().equals(login))
				return user;
		}
		return null;
		
		/*
		Collection<User> c = users.values();
		   
	    //obtain an Iterator for Collection
	    Iterator<User> itr = c.iterator();
	   
	   
	    User user;
	    //iterate through HashMap values iterator
	    while(itr.hasNext()){
	    	user=itr.next();
	    	if(user.getLogin().equals(login)){
	    		return user;
	    	}
	    }
	    return null;
	    */
	}

	@Override
	public void grantAccess(User local, User remote) {
		Key remoteKey=getUserByMail(remote.getEmail()).getKey();
		users.get(local.getKey()).getGrantedUsers().add(remoteKey);
		users.get(remoteKey).getGrantedUsers().add(local.getKey());
	}

	@Override
	public void revokeAccess(User local, User remote) {	
		Key remoteKey=getUserByMail(remote.getEmail()).getKey();
		users.get(local.getKey()).getGrantedUsers().remove(remoteKey);
		users.get(remoteKey).getGrantedUsers().remove(local.getKey());
	}

	@Override
	public List<User> getGrantedUsers(User local) {
		
		Set<Key> grantedUsersKeys = users.get(local.getKey()).getGrantedUsers();
		List<User> grantedUsers = new ArrayList<User>();
		
		for (Key grantedUserKey : grantedUsersKeys) {
			
			User grantedUser=users.get(grantedUserKey);
			User shrinkedUser=new User();
			shrinkedUser.setEmail(grantedUser.getEmail());
			shrinkedUser.setFirstName(grantedUser.getFirstName());
			shrinkedUser.setLastName(grantedUser.getLastName());
			grantedUsers.add(shrinkedUser);
					
		}
		
		return grantedUsers;
	}

	@Override
	public User getUserByMail(String mail) {
		for (User user : users.values()) {
			if(user.getEmail().equals(mail))
				return user;
		}
		return null;
	}
	



	

}
